﻿/*******************************************************************************
 * Copyright © 2016
 * 
 * Description: MVC快速开发平台  FROM http://xmwxgn.com
 *
*********************************************************************************/
using ZFine.Code;
using ZFine.Domain.Entity.Gas;
using ZFine.Domain.IRepository.Gas;
using ZFine.Repository.Gas;
using System;
using System.Collections.Generic;
using System.Linq;

namespace ZFine.Application.Gas
{
    public class DiningRecordApp
    {
        private IDiningRecordRepository service = new DiningRecordRepository();

        public List<DiningRecordEntity> GetList(Pagination pagination, string DiningSpan,string IDNumber,string RName, string StartDate, String EndDate)
        {
            var expression = ExtLinq.True<DiningRecordEntity>();
            if (!string.IsNullOrEmpty(DiningSpan))
            {
                expression = expression.And(t => t.DiningSpan == DiningSpan);
            }
            if (!string.IsNullOrEmpty(IDNumber))
            {
                expression = expression.And(t => t.IDNumber.Contains(IDNumber));
            }
            if (!string.IsNullOrEmpty(RName))
            {
                expression = expression.And(t => t.RName.Contains(RName));
            }
            if (!string.IsNullOrEmpty(StartDate))
            {
                DateTime start = DateTime.Parse(StartDate + " 00:00:00");
                expression = expression.And(t => t.DiningTime >= start);
            }
            if (!string.IsNullOrEmpty(EndDate))
            {
                DateTime end = DateTime.Parse(EndDate + " 23:59:59");
                expression = expression.And(t => t.DiningTime <= end);
            }
            return service.FindList(expression, pagination);
        }
        public List<DiningRecordEntity> GetList( string IDNumber = "")
        {
            var expression = ExtLinq.True<DiningRecordEntity>();
            if (!string.IsNullOrEmpty(IDNumber))
            {
                expression = expression.And(t => t.IDNumber == IDNumber);
            }
            return service.IQueryable(expression).OrderBy(t => t.IDNumber).ToList();
        }
        /// <summary>
        /// 获得列表
        /// </summary>
        public float GetMonthAmount(int year,int month)
        {
            var expression = ExtLinq.True<DiningRecordEntity>();
            if (year>0)
            {
                expression = expression.And(t => t.Year == year);
            }
            if (month > 0)
            {
                expression = expression.And(t => t.Month == month);
            }
            return service.IQueryable(expression).Select(a=>a.Amount).DefaultIfEmpty().Sum();
        }
        /// <summary>
        /// 根据身份证号获得列表
        /// </summary>
        public IQueryable<DiningRecordEntity> GetIQueryable(string IDNumber = "")
        {
            var expression = ExtLinq.True<DiningRecordEntity>();
            if (!string.IsNullOrEmpty(IDNumber))
            {
                expression = expression.And(t => t.IDNumber == IDNumber);
            }
            return service.IQueryable(expression);
        }
        /// <summary>
        /// 获得数量
        /// </summary>
        public int GetCount()
        {
            return service.IQueryable().Count();
        }
        /// <summary>
        ///  按年获得数量
        /// </summary>
        public int GetCount(int Year)
        {
            return service.IQueryable().Where(a => a.Year == Year).Count();
        }
        /// <summary>
        ///  按年月获得数量
        /// </summary>
        public int GetCount(int Year, int Month)
        {
            return service.IQueryable().Where(a => a.Year == Year && a.Month == Month).Count();
        }
        public DiningRecordEntity GetForm(string keyValue)
        {
            return service.FindEntity(keyValue);
        }
        public void DeleteForm(string keyValue)
        {
            //if (service.IQueryable().Count(t => t.F_ParentId.Equals(keyValue)) > 0)
            //{
            //    throw new Exception("删除失败！操作的对象包含了下级数据。");
            //}
            //else
            //{
            //    service.Delete(t => t.F_Id == keyValue);
            //}
        }
        public void SubmitForm(DiningRecordEntity organizeEntity, string keyValue)
        {
            if (!string.IsNullOrEmpty(keyValue))
            {
                organizeEntity.Modify(keyValue);
                service.Update(organizeEntity);
            }
            else
            {
                organizeEntity.Create();
                service.Insert(organizeEntity);
            }
        }
        /// <summary>
        ///  月份关联的就餐数（默认前12个月）
        /// </summary>
        public List<string> GetCountGroupByMonth()
        {
            List<string> resultList = new List<string> ();
            DateTime dt = DateTime.Now;
            string[] arrMonth = new string[12];
            int[] arrDiningCount = new int[12];
            for (int i=-11; i <= 0; i++)
            {
                int curYear = dt.AddMonths(i).Year;
                int curMonth = dt.AddMonths(i).Month;
                arrMonth[i+11] = curMonth + "月";
                arrDiningCount[i + 11] = GetCount(curYear, curMonth);
            }
            string strMonth = string.Join(",", arrMonth);
            string strDiningCount = string.Join(",", arrDiningCount);
            resultList.Add(strMonth);
            resultList.Add(strDiningCount);
            return resultList;
        }
        /// <summary>
        ///  本周的就餐数（默认本周开始到当前日）
        /// </summary>
        public List<string> GetCountGroupByWeek()
        {
            List<string> resultList = new List<string>();
            string[] arrDay = new string[12];
            int[] arrDiningCountByDay = new int[12];
            // 当前日期为一周的第几天
            for (int i = -Convert.ToInt16(DateTime.Now.DayOfWeek); i <= 0; i++)
            {
                DateTime dt = DateTime.Now.AddDays(i);
                arrDay[i + 11] = dt.Month + "月" + dt.Day + "日";
                arrDiningCountByDay[i + 11] = service.IQueryable().Where(a => a.Year == dt.Year && a.Month == dt.Month && a.Day == dt.Day).Count();
            }
            string strDay = string.Join(",", arrDay);
            string strDiningCount = string.Join(",", arrDiningCountByDay);
            resultList.Add(strDay);
            resultList.Add(strDiningCount);
            return resultList;
        }
        /// <summary>
        /// 客户端插入
        /// </summary>
        /// <param name="chargeRecordEntity"></param>
        public void ClientInsert(List<DiningRecordEntity> l_diningRecordEntity)
        {
            foreach (var diningRecordEntity in l_diningRecordEntity)
            {
                diningRecordEntity.ClientCreate();
            }
            service.Insert(l_diningRecordEntity);
        }
    }
}
